<template>
    <addEditBaseStudentView ref="addEditBaseStudentView" />
    <editStudentExcelFile ref="editStudentExcelFile" />
    <addFileView ref="addFileView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
                        <el-select v-model="form.gradeId" autocomplete="off" @change="filterGradeId" clearable
                            style="width: 192px;" placeholder="请选择所在年级">
                            <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所在班级" :label-width="formLabelWidth" prop="classId">
                        <el-select v-model="form.classId" autocomplete="off" placeholder="请选择所在班级" clearable
                            style="width: 192px;" @change="serachHandle">
                            <el-option v-for="item in classInfos" :key="item.classId" :value="item.classId"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="学生姓名">
                        <el-input placeholder="请输入学生姓名" v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="学生学号">
                        <el-input placeholder="请输入学生学号" v-model="form.studentNumber"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" :label-width="formLabelWidth" prop="gradeId">
                        <el-select v-model="form.status" autocomplete="off" style="width: 192px;" placeholder="请选择状态"
                            clearable @change="statusHandle">
                            <el-option v-for="item in statusData" :key="item.value" :value="item.label"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="性别" :label-width="formLabelWidth" prop="gradeId">
                        <el-select v-model="form.sexName" autocomplete="off" @change="sexHandle" style="width: 192px;"
                            placeholder="请选择状态" clearable>
                            <el-option v-for="item in sexData" :key="item.value" :value="item.label"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="serach_btn" type="primary" plain @click="onSubmit"
                            @keyup.enter="searchEnter">搜索</el-button>
                        <el-button class="add_btn" type="success" plain @click="add">添加</el-button>
                        <el-button class="export_btn" type="warning" plain @click="exportExcel"
                            v-loading.fullscreen.lock="fullscreenLoading">导出报表</el-button>
                        <el-button class="export_btn" type="danger" plain @click="importExcel">批量创建<el-icon
                                class="el-icon--right">
                                <Upload />
                            </el-icon></el-button>
                        <el-button class="export_btn" type="info" plain @click="importEditExcel">批量更新<el-icon
                                class="el-icon--right">
                                <Upload />
                            </el-icon></el-button>
                    </el-form-item>

                </el-form>
            </div>
            <el-table size="default" :data="list" style="width: 100%" :row-class-name="tableRowClassName">
                <el-table-column prop="id" label="id" width="90" />
                <el-table-column prop="classId" label="班级">
                    <template #default="scope">
                        高{{ scope.row.gradeId }}级({{ scope.row.classId }})班
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="姓名" />
                <el-table-column prop="studentNumber" label="学号" />
                <el-table-column prop="sex" label="性别">
                    <template #default="scope">
                        {{ scope.row.sex == 1 ? "男" : scope.row.sex == 2 ? "女" : "未知" }}
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="idCard" label="身份证号" />
                <el-table-column prop="mobile" label="联系电话" /> -->
                <el-table-column prop="status" label="状态">
                    <template #default="scope">
                        {{
                    scope.row.status == 0
                        ? "在读"
                        : scope.row.status == 2
                            ? "休学"
                            : scope.row.status == 3
                                ? "退学"
                                : "在读"
                }}
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="idCard" label="身份证号" /> -->
                <el-table-column label="操作" width="140px">
                    <template style="display: block" #default="scope">
                        <el-button class="edit_btn" size="small" type="success" @click="editBaseStudent(scope.row)">编辑
                        </el-button>
                        <el-button class="del_btn" size="small" type="danger"
                            @click="deleteBaseStudent(scope.row.id)">删除
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <el-config-provider :locale="locale">
                <el-pagination :total="total" :page-size="limit" :current-page="page"
                    :page-sizes="[5, 10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
                    @size-change="changePageSize" @current-change="changeCurrentPage">
                </el-pagination>
            </el-config-provider>
        </div>
        <bottomView></bottomView>
    </div>
</template>
<script>
import addEditBaseStudentView from "@/components/baseInfo/addEditBaseStudent.vue";
import bottomView from "@/components/bottom.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage, ElLoading } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
    name: "BaseStudentListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            gradeIds: [],
            limit: 10,
            fullscreenLoading: false,
            list: [],
            map: {},
            classInfos: [],
            form: {
                studentNumber: "",
                classId: "",
                gradeId: "",
                password: "",
                name: "",
                examineNumber: "",
                idCard: "",
                fatherMobile: "",
                motherMobile: "",
                fatherName: "",
                motherName: "",
                address: "",
                fatherWorkUnit: "",
                motherWorkUnit: "",
                mobile: "",
                alias: "",
                photo: "",
                politicalOutlook: "",
                incomeDate: "",
                nation: "",
                graduateSchool: "",
                admissionScore: "",
                type: "",
                incomeCode: "",
                beforeName: "",
                nationality: "",
                modeStudy: "",
                oneCardNumber: "",
                dormitoryNumber: "",
                englishName: "",
                status: "",
                sex: "",
                sexName: "",
            },
            statusData: [
                {
                    value: 0,
                    label: '在读',
                },
                {
                    value: 2,
                    label: '休学',
                },
            ],
            sexData: [
                {
                    value: 1,
                    label: '男',
                },
                {
                    value: 2,
                    label: '女',
                },
                {
                    value: 3,
                    label: '未知',
                },
            ],
        };
    },
    components: {
        addEditBaseStudentView,
        addFileView, ElLoading, bottomView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage,
        Upload,
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getBaseStudentList();
        this.getGradeIds();
        this.getBaseClassInfoList();

    },
    watch: {
        $route() {
            this.getGradeIds();
            this.getBaseClassInfoList();

            this.getBaseStudentList();
        },
    },
    methods: {
        getBaseClassInfoList() {
            httpService("/javaApi/baseClassInfo/getAllEffectClass", {}, "get").then(
                (data) => {
                    this.map = data;
                }
            );
        },
        serachHandle() {
            this.page = 1
            this.getBaseStudentList();
        },
        // statusHandle(){},
        sexHandle(val) {
            console.log("性别", val);
            this.sexName = val
            if (val == '男') {
                this.form.sex = 1
            } else if (val == '女') {
                this.form.sex = 2
            } else if (val == '未知') {
                this.form.sex = 0
            } else {
                this.form.sex = ""
            }
            this.page = 1
            this.getBaseStudentList();
        },
        filterGradeId(val) {
            this.classInfos = this.map[val];
            this.form.classId = ""
            this.page = 1
            this.getBaseStudentList();
        },
        getGradeIds() {
            let date = new Date();
            let nowYear = parseInt(date.getFullYear());
            let nowMonth = parseInt(date.getMonth() + 1);
            let gradeIds = [];
            if (nowMonth > 6) {
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
                gradeIds.push(nowYear);
            } else {
                gradeIds.push(nowYear - 3);
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
            }
            this.gradeIds = gradeIds;
        },
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getBaseStudentList();
            }
        },
        add() {
            this.$refs.addEditBaseStudentView.title = "添加学生";
            this.$refs.addEditBaseStudentView.getGradeIds();
            this.$refs.addEditBaseStudentView.getBaseClassInfoList();
            this.$refs.addEditBaseStudentView.dialogFormVisible = true;
        },
        editBaseStudent(obj) {
            this.$refs.addEditBaseStudentView.title = "修改信息";
            this.$refs.addEditBaseStudentView.getGradeIds();
            this.$refs.addEditBaseStudentView.getBaseClassInfoList();
            this.$refs.addEditBaseStudentView.getBaseStudentById(obj.id);
            this.$refs.addEditBaseStudentView.dialogFormVisible = true;
        },
        deleteBaseStudent(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService(
                        "/javaApi/baseStudent/deleteById",
                        "id=" + id,
                        "post"
                    ).then((data) => {
                        if (data.code == 200) {
                            ElMessage.success("删除成功");
                            this.getBaseStudentList();
                        } else {
                            ElMessage.error("删除失败，请重试！");
                        }
                    });
                })
                .catch(() => { });
        },
        reloadData() {
            this.page = 1;
            this.getBaseStudentList();
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量创建";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/baseStudent/importExcel";
        },
        importEditExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量更新";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/baseStudent/importEditExcel";
        },
        exportExcel() {
            this.fullscreenLoading = true;
            httpService(
                "/javaApi/baseStudent/exportExcel",
                {
                    studentNumber: this.form.studentNumber,
                    password: this.form.password,
                    name: this.form.name,
                    classId: this.form.classId ? this.form.classId : this.form.classId = "",
                    gradeId: this.form.gradeId ? this.form.gradeId : "",
                    examineNumber: this.form.examineNumber,
                    idCard: this.form.idCard,
                    fatherMobile: this.form.fatherMobile,
                    motherMobile: this.form.motherMobile,
                    fatherName: this.form.fatherName,
                    motherName: this.form.motherName,
                    address: this.form.address,
                    fatherWorkUnit: this.form.fatherWorkUnit,
                    motherWorkUnit: this.form.motherWorkUnit,
                    mobile: this.form.mobile,
                    alias: this.form.alias,
                    photo: this.form.photo,
                    politicalOutlook: this.form.politicalOutlook,
                    incomeDate: this.form.incomeDate,
                    nation: this.form.nation,
                    graduateSchool: this.form.graduateSchool,
                    admissionScore: this.form.admissionScore,
                    type: this.form.type,
                    incomeCode: this.form.incomeCode,
                    beforeName: this.form.beforeName,
                    nationality: this.form.nationality,
                    modeStudy: this.form.modeStudy,
                    oneCardNumber: this.form.oneCardNumber,
                    dormitoryNumber: this.form.dormitoryNumber,
                    englishName: this.form.englishName,
                    status: this.form.status,
                },
                "get"
            ).then((data) => {
                this.fullscreenLoading = false;
                if (data.code == 200) {
                    // console.log(data.message);
                    if (data.message.indexOf("http") != -1 && data.message.indexOf("/javaApi") != -1) {
                        data.message = data.message.replace("/javaApi", '')
                        data.message = data.message.replace("http", 'https')
                    }
                    // console.log(data.message);
                    // window.location.href = data.message
                    window.open(data.message, "_blank");
                }
            });
        },
        onSubmit() {
            this.getBaseStudentList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getBaseStudentList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getBaseStudentList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getBaseStudentList() {
            let stuType = ''
            this.form.status == "在读" ? stuType = 0 : this.form.status == "休学" ? stuType = 2 : this.form.status == '' ? stuType = '' : stuType = ''

            httpService(
                "/javaApi/baseStudent/list/" + this.page + "/" + this.pageSize,
                {
                    studentNumber: this.form.studentNumber,
                    sex: this.form.sex,
                    password: this.form.password,
                    classId: this.form.classId ? this.form.classId : this.form.classId = "",
                    gradeId: this.form.gradeId ? this.form.gradeId : this.form.gradeId = "",
                    name: this.form.name,
                    examineNumber: this.form.examineNumber,
                    idCard: this.form.idCard,
                    fatherMobile: this.form.fatherMobile,
                    motherMobile: this.form.motherMobile,
                    fatherName: this.form.fatherName,
                    motherName: this.form.motherName,
                    address: this.form.address,
                    fatherWorkUnit: this.form.fatherWorkUnit,
                    motherWorkUnit: this.form.motherWorkUnit,
                    mobile: this.form.mobile,
                    alias: this.form.alias,
                    photo: this.form.photo,
                    politicalOutlook: this.form.politicalOutlook,
                    incomeDate: this.form.incomeDate,
                    nation: this.form.nation,
                    graduateSchool: this.form.graduateSchool,
                    admissionScore: this.form.admissionScore,
                    type: this.form.type,
                    incomeCode: this.form.incomeCode,
                    beforeName: this.form.beforeName,
                    nationality: this.form.nationality,
                    modeStudy: this.form.modeStudy,
                    oneCardNumber: this.form.oneCardNumber,
                    dormitoryNumber: this.form.dormitoryNumber,
                    englishName: this.form.englishName,
                    status: stuType,
                },
                "get"
            ).then((data) => {
                this.list = data.list;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 5px 10px 10px;
    /* width: 100%; */
}

.app-search {}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 204px);

}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.add_btn {
    background-color: #00c78c;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.edit_btn {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.del_btn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}
</style>
